package com.cjh.admin.controller;

import com.cjh.common.entity.Role;
import com.cjh.common.response.Result;
import com.cjh.common.service.RoleService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjh
 * @since 2021-11-15
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleService iRoleService;

    @ApiOperation("获取所有菜单")
    @GetMapping("/getAllRole")
    public Result getAllRole(){
        return iRoleService.getAllRole();
    }

    @ApiOperation("根据角色id获取菜单")
    @GetMapping("/getRoleByRoleId")
    public Result getRoleByRoleId(@RequestParam Integer id){
        return iRoleService.getRoleByRoleId(id);
    }

    @ApiOperation("根据角色id修改菜单")
    @PostMapping("/updateRoleByRoleId")
    public Result updateRoleByRoleId(@RequestBody Map map){
        HashSet<Integer> set = new HashSet((Collection) map.get("set"));
        return iRoleService.updateRoleByRoleId((Integer) map.get("id"), set);
    }
    @RequiresPermissions(value = {"updateRole"})
    @ApiOperation("修改角色")
    @PostMapping("/updateRole")
    public Result updateRole(@RequestBody Role role){
        return iRoleService.updateRole(role);
    }

    @RequiresPermissions(value = {"addRole"})
    @ApiOperation("增加角色")
    @PostMapping("/addRole")

    public Result addRole(@RequestBody Role role){
        return iRoleService.addRole(role);
    }
    @RequiresPermissions(value = {"deleteRole"})

    @ApiOperation("删除角色")
    @PostMapping("/deleteRoleById")
    public Result deleteRoleById(@RequestBody Map<String,Integer> map) {
        return iRoleService.deleteRoleById(map.get("id"));
    }

    @RequiresPermissions(value = {"deleteRole"})
    @ApiOperation("删除多个角色")
    @PostMapping("/deleteRoleByIdList")
    public Result deleteRoleByIdList(@RequestBody List<Integer> list) {
        return iRoleService.deleteRoleByIdList(list);
    }
}
